import Vue from 'vue'
import permision from "../cxUtils/permission.js"


//选择图片
Vue.prototype.$chooseImage = (count = 1) => {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: count, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], //从相册选择
			success: function(res) {
				let tempFiles = res.tempFiles.map(item => {
					return {
						path: item.path,
						size: item.size,
						name: item.name,
						type: item.type
					}
				})

				console.log(tempFiles)
				resolve(tempFiles);
			},
			fail(err) {
				reject(err);
			}
		});

	})
}

Vue.prototype.$preView = (url) => {

	return new Promise((resolve, reject) => {
		// 预览图片
		uni.previewImage({
			urls: url,
			longPressActions: {
				itemList: ['发送给朋友', '保存图片', '收藏'],
				success: function(data) {
					console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) +
						'张图片');
					resolve(data.tapIndex);
				},
				fail: function(err) {
					console.log(err.errMsg);
					reject(err);
				}
			}
		});
	});


}

// 获取定位
// 获取定位gcj02/wgs84
Vue.prototype.$getLocation = () => {
	return new Promise((resolve) => {
		// console.log("result--$getLocation--")
		console.log("result--$getLocation--")
		// #ifndef H5
		uni.getLocation({
			// type: 'gcj02',
			type: 'gcj02',
			geocode: true,
			success(res) {
				console.log(res);
				Vue.prototype.$setLatitude(res.latitude)
				Vue.prototype.$setLongitude(res.longitude)
				resolve(res);
			},
			fail(error) {
				uni.showToast({
					icon: 'none',
					title: JSON.stringify(error),
					duration: 2000,
				})
				// Vue.prototype.$getSetting();
				resolve("error");
			},
		});
		// #endif
		// #ifdef H5
		uni.getLocation({
			type: 'gcj02',
			geocode: true,
			success(res) {
				console.log(res);
				Vue.prototype.$setLatitude(res.latitude)
				Vue.prototype.$setLongitude(res.longitude)

				resolve(res);
			},
			fail(error) {
				uni.showToast({
					icon: 'none',
					title: JSON.stringify(error),
					duration: 2000,
				})
				// Vue.prototype.$getSetting();
				resolve("error");
			},
		});

		// #endif


	})

}


// 1.获取设置信息-用户权限列表
Vue.prototype.$getSetting = () => {

	// #ifdef APP
	let isIos = (plus.os.name == "iOS")
	let permisionID = "location"
	if (isIos) {
		// 是苹果手机
		var result = permision.judgeIosPermission(permisionID)
		console.log(result);
		if (!result) {
			var strStatus = (result) ? "已" : "未"
			uni.showModal({
				content: "定位" + '权限' + "未获得授权",
				showCancel: false
			});
		}

	} else {
		// 安卓手机
		var result = permision.requestAndroidPermission(permisionID);
		var strStatus
		if (result != 1) {
			strStatus = "未获得授权"
		}
		// uni.showModal({
		// 	content: permisionID + strStatus,
		// 	showCancel: false
		// });
		uni.showModal({
			content: "定位" + strStatus,
			success: function(res) {
				if (res.confirm) {
					console.log('用户点击确定');

					var main = plus.android.runtimeMainActivity(); //获取activity
					var Intent = plus.android.importClass('android.content.Intent');
					var Settings = plus.android.importClass('android.provider.Settings');
					var intent = new Intent(Settings
						.ACTION_LOCATION_SOURCE_SETTINGS
					); //可设置http://ask.dcloud.net.cn/question/14732这里所有Action字段
					main.startActivity(intent);


				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});

	}
	// #endif


	// uni.getSetting({
	// 	success: res => {
	// 		console.log('用户权限列表:', res.authSetting)
	// 		if (res.authSetting['scope.userLocation']) {
	// 			console.log('已授权userLocation')
	// 			// 选择位置信息	
	// 		} else {
	// 			console.log('用户未授权userLocation')
	// 			//2.用户第一次进来发起授权
	// 			uni.showModal({
	// 				title: '提示',
	// 				content: '当前定位未开启，请点击确定手动开启定位',
	// 				duration: 3000,
	// 				success: (res) => {
	// 					if (res.confirm) {
	// 						Vue.prototype.$openSetting();
	// 					} else if (res.cancel) {
	// 						uni.showToast({
	// 							title: '你拒绝了授权，无法获取周边信息',
	// 							duration: 2000,
	// 							icon: "none"
	// 						});
	// 					}
	// 				}
	// 			});
	// 		}
	// 	}
	// })
};

// 4.打开设置
Vue.prototype.$openSetting = res => {
	uni.openSetting({
		success: (res) => {
			if (res.authSetting['scope.userLocation']) {
				// 5.用户在设置中点击了允许，调用选择位置信息函数
				Vue.prototype.$getLocation();
			} else {

				// 5.用户在设置中点击了不允许，展示拒绝授权信息
				uni.showToast({

					title: '你拒绝了授权，无法操作内容',
					icon: "none",
					duration: 3000,
				})
			}
		},
		fail: (err) => {
			console.log("打开设置失败", err)
		}
	})
};


Vue.prototype.$getGzhCode = (appid, redirectUrl) => {
	return new Promise((resolve, reject) => {
		//获取当前页面的url
		let link = window.location.href;
		console.log(link, 'link');
		let code = null;
		// 判断link中有没有code=字符串，  
		if (link.indexOf('code=') == -1) {
			//没有code= 发请求
			let uri = encodeURIComponent(redirectUrl);
			let authURL =
				`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`;
			window.location.href = authURL;
		} else {
			//回调函数已经执行 返回的链接存在code= 地址解析
			let temp = link.split("code=")[1];
			code = temp.split("&")[0];
			console.log(code, 'code');
			//存储code
			uni.setStorageSync('code', code);
			//对后台发送请求传参code获取token和用户信息
			resolve(code);
		}

	})
};